package com.cictec.middleware.gps.engine.position.handler.operateState;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cictec.middleware.commons.utils.Constants;
import com.cictec.middleware.commons.utils.DateUtils;
import com.cictec.middleware.commons.utils.PMSUtils;
import com.cictec.middleware.commons.utils.log.MultiLog;
import com.cictec.middleware.core.entity.Bus;
import com.cictec.middleware.gps.engine.BusInstance;
import com.cictec.middleware.gps.engine.exception.BusPositionHandlerException;
import com.cictec.middleware.gps.engine.position.handler.BusPositionDriftHandler;
import com.cictec.middleware.gps.engine.utils.MultiLogUtil;
import com.cictec.middleware.gps.service.BusService;

/**
 * 运营状态处理-【强制非运营】影响的运营状态
 * @author ql
 * @date 2017年9月18日
 */
@Service
public class NonOperationStateHandler implements OperationStateHandler{

	private static Logger logger = LoggerFactory.getLogger(BusPositionDriftHandler.class);
	
	@Autowired
	private BusService busService;
	
	@Override
	public void onHandler(BusInstance busInstance) throws BusPositionHandlerException {
		MultiLog log = MultiLogUtil.getPositionJudgeLog(busInstance);
		/* 强制非运营判定,是否进行过强制非运营操作 start */
		Bus bus = busInstance.getBus();
		
		String forceNonoper = PMSUtils.isNull(bus.getBusForceNonOper());	// 强制非运营字段
		Date forceNonoperExpired = bus.getBusForceNonOperExpired();			// 强制非运营过期时间
		
		if(forceNonoper.equals(Constants.FNO_COMMAND_CODE_VAL_FORCE) && null != forceNonoperExpired){
			// 强制非运营
			Date time = busInstance.getTime();				// 当前设备上传数据时间
			
			if(time.getTime() > forceNonoperExpired.getTime()){
				logger.debug("\t强制非运营时间过期 【运营时间状态】【IN_OPER_TIME】 设置为：运营");
				busInstance.setAttributeInOperateTime(Constants.VAL_BUS_OPER_STATUS_1);
				
				
				bus.setBusForceNonOper(Constants.VAL_NO);								// 取消强制非运营
				bus.setBusStatus(Constants.VAL_BUS_OPER_STATUS_1);						// 车辆状态改为运营
				busService.updateForceNonoperStatus(bus);
			}else{
				logger.debug("\t{}强制非运营中，强制非运营结束时间 {}", bus.getBusPlateNumber(), DateUtils.formatDateTimes(bus.getBusForceNonOperExpired()));
				log.debug("\t{}强制非运营中，强制非运营结束时间 {}", bus.getBusPlateNumber(), DateUtils.formatDateTimes(bus.getBusForceNonOperExpired()));
				busInstance.setAttributeInOperateTime(Constants.VAL_BUS_OPER_STATUS_0);
			}
		}else if(forceNonoper.equals(Constants.FNO_COMMAND_CODE_VAL_CANCEL)){
//			busInstance.setStatusInOperateTime(Constants.VAL_BUS_OPER_STATUS_1);
		}
		/* 强制非运营判定,是否进行过强制非运营操作 end */
		
	}

}
